'use strict';

const Controller = require('egg').Controller;

//controller 接收前端输入的信息，把处理的结果给前端响应
class MusicController extends Controller {
    async list() {
        const { ctx ,app,service} = this;
        //页码 pagenum   每页的条数  limit
        //get/delete ctx.query  post/put  ctx.request.body
        let {pagenum=1,limit=1} = ctx.query;
        let list = await service.student.list(pagenum,limit)
        ctx.body = list;
    }
    //添加
    async add(){
        const {ctx,service} = this;
        let {username,num,classNum,password,classRoom} = ctx.request.body;
        if(username&&num&&classNum&&password&&classRoom){
            try{
                //尝试添加
                await service.student.add(username,num,classNum,password,classRoom);
                ctx.body = {
                    code:1,
                    msg:'成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //删除
    async del(){
        let {ctx,service} = this;
        let {id} = ctx.query;
        if(id){
            try{
                await service.student.del(id);
                ctx.body = {
                    code:1,
                    msg:'删除成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }

    //更新
    async update(){
        const {ctx,service} = this;
        let {username,num,classNum,password,classRoom,id} = ctx.request.body;
        if(username&&num&&classNum&&password&&classRoom &&id){
            try{
                //尝试添加
                await service.student.update(username,num,classNum,password,classRoom,id);
                ctx.body = {
                    code:1,
                    msg:'成功'
                }
            }catch(e){
                ctx.body = {
                    code:0,
                    msg:e
                }
            }
        }else{
            ctx.body = {
                code:2,
                msg:'参数有误'
            }
        }
    }
}

module.exports = MusicController;
